package edu.hcnu.controller;

import edu.hcnu.ApiResult;
import edu.hcnu.entity.Car;
import edu.hcnu.service.CarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: Administrator
 * @time: 2023/4/29 19:18
 */

@Api(tags = "车次接口")
@RestController
@RequestMapping("car")
public class CarController {

    @Autowired
    private CarService carService;

    /**
     * @param car
     * @return
     */
    @ApiOperation(value = "添加车次")
    @PostMapping("add")
    ApiResult add(@RequestBody Car car){
        String res=carService.add(car);
        if("success".equals(res)){
            return  ApiResult.success(200,"添加车次成功");
        }
        return  ApiResult.fail(400,res);
    }

    @ApiOperation("查询所有的车次")
    @GetMapping("all")
    ApiResult getAll(){
        List<Car> list=carService.getAllCar();
        if(list!=null && list.size()>0){
            return  ApiResult.success(200,"查询所有车次成功",list);
        }
        return  ApiResult.fail(400,"查询车次失败");
    }
    @ApiOperation(value = "删除车次")
    @DeleteMapping("delete/{cid}")
    ApiResult delete(@PathVariable("cid")String cid){
        boolean res=carService.deleteCar(cid);
        if(res){
            return  ApiResult.success(200,"删除车次成功");
        }
        return  ApiResult.fail(400,"删除车次失败");
    }
}
